// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-28 10:00
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.Greedy;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LargeNumT {

    public String largestNumber(int[] nums) {
        // 从给定的整形数组中挑选数字进行组合，组合的数字应该是最大的
        // 对整形数组中的数字进行排序，从高位开始进行降序，如果高位相同则根据低位进行降序排序
        StringBuilder builder = new StringBuilder(); // 用于生成String
        Integer[] res = sortNum(nums);
        if (res[0] == 0) return "0";
        for (int i = 0; i < res.length; i++) {
            builder.append(res[i]);
        }
        return builder.toString();
    }

    private Integer[] sortNum(int[] nums) {
        // 根据数字序进行排序
        Integer[] ans = Arrays.stream(nums).boxed().sorted((x, y) -> {
                    long sx = 10, sy = 10;
                    while (sx <= x) {
                        sx *= 10;
                    }
                    while (sy <= y) {
                        sy *= 10;
                    }
                    return (int) (-sy * x - y + sx * y + x);
                }
        ).collect(
                Collectors.toList()).toArray(new Integer[nums.length]);

        return ans;
    }
}
